import { getBannedList } from '@/apis/accessManagement/user'
import { cancelBanned } from '@/apis/accessManagement/user'
import ExcelTable from '@/components/exportExcel'
import { ActionType, ProFormInstance } from '@ant-design/pro-components'
import {Button, Modal, Form, Input, Flex, Tree, message} from 'antd'
import {useRef, useState} from 'react'

const BannedList: React.FC = () => {
    useRef<ActionType>()
    useRef<ProFormInstance>()
    const { TextArea } = Input
    const actionRef = useRef();
    const [form] = Form.useForm()
    const [cancelID, setCancelID] = useState('')
    const [messageApi, contextHolder] = message.useMessage();
    const [loading, setLoading] = useState(false);

    // 提交解除封禁
    const subCancel = async (record:any) => {
        setLoading(true)
        const handleID:string[] = [record.userID]
        try {
            const response = await cancelBanned({
                userIDs: handleID,
            });
            if (response || response.data) {
                const { errCode, errMsg } = response;
                if (response?.errCode === 0) {
                    message.success('解除封禁成功');
                    actionRef.current?.reloadAndRest()
                } else {
                    message.error(`提交失败: ${response?.errMsg || '未知错误'}`);
                }
            } else {
                message.error('响应数据格式错误');
            }
        } catch (error) {
            console.error('Error:', error);
        } finally {
            setLoading(false)
        }
    };

    return (
        <>
            {contextHolder}
            <ExcelTable
                actionRef={actionRef}
                requestFn={ params => {
                    const { pageNum,pageSize,userID, ...newParams } = params; // 处理所需参数名
                    const data = getBannedList({
                        ...newParams,
                        pagination: {pageNumber:pageNum,showNumber:pageSize}, 
                        keyword: params.userID,
                    })
                    return data
                }}
                rowSelection={false}
                toolBarRenderFn={() => [
                    // <Button key="add" onClick={() => {
                    // }}>11</Button>
                ]}
                columns={[
                    {
                        title: '序号',
                        valueType: 'index',
                        hideInSearch: true
                    },
                    {
                        title: '用户',
                        hideInSearch: true,
                        dataIndex: 'nickname',
                    },
                    {
                        title: '用户ID',
                        dataIndex: 'userID',
                    },
                    {
                        title: '性别',
                        dataIndex: 'gender',
                        hideInSearch: true,
                        valueType: 'select',
                        valueEnum: {
                            1: '男',
                            2: '女',
                        }
                    },
                    {
                        title: '注册时间',
                        dataIndex: 'createTime',
                        hideInSearch: true,
                        valueType: 'dateTime'
                    },
                    {
                        title: '操作人',
                        dataIndex: 'opUserID',
                        hideInSearch: true,
                    },
                    {
                        title: '操作',
                        key: 'option',
                        valueType: 'option',
                        render: (_, record) => [
                            <a key="resetPwd" onClick={() => {
                                Modal.warning({
                                    title: '提示',
                                    content: '确定解除封禁该用户吗?',
                                    okText: '确定',
                                    okCancel: true,
                                    onOk() {
                                        // setCancelID(record.userID)
                                        subCancel(record)
                                    }
                                })
                            }}>解除封禁</a>,
                        ]
                    }
                ]}/>
        </>
    )
}

export default BannedList
